package org.exez.modules.visitor.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.exez.common.api.vo.Result;
import org.exez.common.system.query.QueryGenerator;
import org.exez.common.aspect.annotation.AutoLog;
import org.exez.common.util.DateUtils;
import org.exez.common.util.oConvertUtils;
import org.exez.modules.visitor.entity.Institution;
import org.exez.modules.visitor.entity.VisitType;
import org.exez.modules.visitor.service.IVisitTypeService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.exez.common.system.base.controller.JeecgController;
import org.exez.modules.visitor.vo.ReservationApplicationVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 到访类型表
 * @Author: exez-boot
 * @Date:   2020-06-04
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "访客到访类型功能接口")
@RestController
@RequestMapping("/visitor/visitType")
public class VisitTypeController extends JeecgController<VisitType, IVisitTypeService> {
	@Autowired
	private IVisitTypeService visitTypeService;

	/**
	 * 到访类型列表查询
	 *
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@AutoLog(value = "到访类型列表查询")
	@ApiOperation(value="到访类型列表查询", notes="到访类型列表查询")
	@ApiResponses({@ApiResponse(code = 200, message = "OK", response = VisitType.class),})
	@GetMapping(value = "/list")
	public Result<?> queryPageList(
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		LambdaQueryWrapper<VisitType> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(VisitType::getId);
		Page<VisitType> page = new Page<>(pageNo, pageSize);
		IPage<VisitType> pageList = visitTypeService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 * 添加到访类型
	 *
	 * @param typeName
	 * @return
	 */
	@AutoLog(value = "添加到访类型")
	@ApiOperation(value="添加到访类型", notes="添加到访类型")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestParam(name = "typeName") String typeName) {
		LambdaQueryWrapper<VisitType> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(VisitType::getTypeName,typeName);

		int count = visitTypeService.count(queryWrapper);
		if (count > 1){
			return Result.error("已存在该类型："+typeName);
		}
		VisitType visitType = new VisitType();
		visitType.setTypeName(typeName);
		visitType.setCreateTime(DateUtils.getDate());
		visitType.setUpdateTime(DateUtils.getDate());
		visitType.setDeleted(1);

		visitTypeService.save(visitType);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑到访类型
	 *
	 * @param id
	 * @param typeName
	 * @return
	 */
	@AutoLog(value = "编辑到访类型")
	@ApiOperation(value="编辑到访类型", notes="编辑到访类型")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestParam(name = "id") Integer id,
						  @RequestParam(name = "typeName") String typeName) {
		LambdaQueryWrapper<VisitType> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(VisitType::getTypeName,typeName);
		queryWrapper.ne(VisitType::getId,id);

		int count = visitTypeService.count(queryWrapper);
		if (count > 1){
			return Result.error("已存在该类型："+typeName);
		}
		VisitType visitType = new VisitType();
		visitType.setId(id);
		visitType.setTypeName(typeName);
		visitType.setUpdateTime(DateUtils.getDate());

		visitTypeService.updateById(visitType);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "到访类型通过id删除")
	@ApiOperation(value="到访类型通过id删除", notes="到访类型通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		visitTypeService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "到访类型通过id查询")
	@ApiOperation(value="到访类型通过id查询", notes="到访类型通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VisitType visitType = visitTypeService.getById(id);
		return Result.ok(visitType);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param visitType
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, VisitType visitType) {
      return super.exportXls(request, visitType, VisitType.class, "到访类型表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, VisitType.class);
  }

}
